package route

import (
	"bc/middleware"
	"bc/middleware/jwt"
	"bc/route/api/v1"
	"github.com/gin-contrib/pprof"
	"github.com/gin-gonic/gin"
)

func InitRouter() *gin.Engine {
	r := gin.New()

	r.Use(middleware.Logger(),gin.Recovery(),middleware.Cors())

	r.Static("/upload", "./upload")
	api_v1 := r.Group("/api/v1")
	api_v1.POST("auth", v1.GetAuth)
	pprof.Register(r) // 性能
	api_v1.Use(jwt.JWT())
	{
		//用户
		api_v1.POST("user", v1.User.Create)
		api_v1.DELETE("user/:uid", v1.User.Del)
		api_v1.PUT("user/:uid", v1.User.Update)
		api_v1.GET("user/:uid", v1.User.First)
		api_v1.GET("users", v1.User.Row)
		//角色
		api_v1.GET("role/:rid", v1.Role.First)
		api_v1.GET("roles", v1.Role.Find)
		api_v1.POST("role", v1.Role.Create)
		api_v1.DELETE("role/:rid", v1.Role.Delete)
		api_v1.PUT("role/:rid", v1.Role.Update)
		//权限
		/*
			api_v1.GET("rule/:ru_id",v1.Rule.First)
			api_v1.GET("rules",v1.Rule.Find)
			api_v1.POST("rule",v1.Rule.Create)
			api_v1.DELETE("rule/:ru_id",v1.Rule.Delete)
			api_v1.PUT("rule/:ru_id",v1.Rule.Update)*/
		//部门
		api_v1.DELETE("dep/:dp_id", v1.Dep.Delete)
		api_v1.POST("dep", v1.Dep.Create)
		api_v1.PUT("dep/:dp_id", v1.Dep.Update)
		api_v1.GET("dep", v1.Dep.Find)
		api_v1.GET("dep/:dp_id", v1.Dep.First)
		//菜单
		api_v1.GET("menu/:m_id", v1.Menu.First)
		api_v1.GET("menu", v1.Menu.Find)
		api_v1.POST("menu", v1.Menu.Create)
		api_v1.DELETE("menu/:m_id", v1.Menu.Delete)
		api_v1.PUT("menu/:m_id", v1.Menu.Update)
		api_v1.GET("nodes", v1.Menu.Nodes)
		api_v1.GET("routers", v1.Routes)

		//计算规格
		api_v1.GET("instance_offer/:io_id", v1.InsOffer.First)
		api_v1.GET("instance_offers", v1.InsOffer.Find)
		api_v1.POST("instance_offer", v1.InsOffer.Create)
		api_v1.DELETE("instance_offer/:io_id/:uuid", v1.InsOffer.Delete)
		api_v1.PUT("instance_offer/:io_id/:uuid", v1.InsOffer.Update)
		api_v1.PUT("instance_offer_state/:uuid", v1.InsOffer.ChangeInstanceOfferingState)

		//操作系统
		api_v1.GET("os/:os_id", v1.OS.First)
		api_v1.GET("os", v1.OS.Find)
		api_v1.POST("os", v1.OS.Create)
		api_v1.DELETE("os/:os_id", v1.OS.Delete)
		api_v1.PUT("os/:os_id", v1.OS.Update)

		api_v1.POST("osn", v1.OS.CreateN)
		//设备
		api_v1.POST("device",v1.Device.Create)
		api_v1.DELETE("device/:d_id",v1.Device.Delete)
		api_v1.GET("devices",v1.Device.Find)
		//区域
		api_v1.POST("zone", v1.Zone.Create)
		api_v1.DELETE("zone/:uuid", v1.Zone.Delete)
		api_v1.PUT("zone/:uuid", v1.Zone.Update)
		api_v1.PUT("zone_state/:uuid", v1.Zone.UpdateState)
		api_v1.GET("zones", v1.Zone.Find)
		api_v1.GET("zone/:uuid", v1.Zone.First)
		//靶机
		api_v1.POST("bj", v1.Bj.Create)
		api_v1.DELETE("bj/:bj_id", v1.Bj.Delete)
		api_v1.PUT("bj/:bj_id", v1.Bj.Update)
		api_v1.GET("bjs", v1.Bj.Find)
		api_v1.GET("bj/:bj_id", v1.Bj.First)
		//场景
		api_v1.POST("scene",v1.Scene.Create)
		api_v1.GET("scene",v1.Scene.Find)
		api_v1.GET("scene/:sc_id",v1.Scene.First)
		api_v1.DELETE("scene/:sc_id",v1.Scene.Delete)
		//镜像
		api_v1.GET("images", v1.Image.Find)
		api_v1.GET("image/:img_id", v1.Image.First)
		api_v1.POST("image", v1.Image.Create)
		api_v1.DELETE("image/:uuid", v1.Image.Delete)
		api_v1.PUT("image/:uuid", v1.Image.Update)
		//2层网络
		api_v1.POST("l2_net", v1.NetWork.Create)
		//api_v1.POST("l2_vxlan_net", v1.NetWork.CreateL2VxlanNetwork)
		api_v1.POST("l2_networks_vlan", v1.NetWork.CreateL2VlanNetwork)
		api_v1.DELETE("l2_net/:uuid", v1.NetWork.Delete)
		api_v1.PUT("l2_net/:uuid",v1.NetWork.Update)
		//3层网络
		api_v1.POST("l3_net", v1.NetWork.CreateL3)
		api_v1.DELETE("l3_net/:uuid", v1.NetWork.L3NetworkDelete)
		//创建云主机
		api_v1.POST("vm", v1.Vm.Create)
		api_v1.POST("vm_temp", v1.Vm.CreateTemp)
		api_v1.GET("vm/address/:sn_id", v1.Vm.GetAddress)
		//镜像仓库服务器相关接口
		api_v1.POST("image_server", v1.ImageServer.Create)
		api_v1.DELETE("image_server/:uuid", v1.ImageServer.Delete)
		//操作日志
		api_v1.GET("log",v1.LogList)

	}
	//回调路由
	api_v1_group := r.Group("")
	CallBackRouter(api_v1_group)
	//r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
	return r
}

func HtmlHander() *gin.Engine {

	r := gin.New()
	r.Use(gin.Logger())
	r.Use(gin.Recovery())

	//r.StaticFS("bc",http.Dir("my_file_system"))
	r.LoadHTMLGlob("dist/*.html")    // 添加入口index.html
	r.LoadHTMLFiles("static/*/*")	// 添加资源路径
	r.LoadHTMLFiles("image/*/*")
	r.Static("static", "./dist/static") 	// 添加资源路径
	r.Static("upload", "./upload/image") 	// 添加资源路径
	r.StaticFile("/", "dist/index.html")

	return r
}
